﻿using Exam.BLL;
using Exam.DBHelper;
using Exam.Model;
using ExaminationWeb.Common;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ExaminationWeb.Controllers
{
    public class CityController : Controller
    {
        //
        // GET: /City/
        ProvinceService provinceService = new ProvinceService();
        CityService cityService = new CityService();
        Exam.BLL.ExtendBLL.City Excityservice = new Exam.BLL.ExtendBLL.City();
        public ActionResult CityList()
        {
            ViewData["ProvinceList"] = provinceService.LoadEntites(e => e.IsActive == 0);
            return View();
        }

        public ActionResult CityUpdate(string id)
        {
            ViewData["ProvinceList"] = provinceService.LoadEntites(e => e.IsActive == 0);
            ViewData["City"] = cityService.LoadEntites(e => e.CityID == Guid.Parse(id)).FirstOrDefault();

            return View();
        }

        public ActionResult CityAdd(string id)
        {
            ViewData["ProvinceList"] = provinceService.LoadEntites(e => e.IsActive == 0);
            return View();
        }
        [AllowAnonymous]
        public JsonResult GetCitysInfo()
        {
            int pageIndex = Request["sEcho"] == null ? 1 : int.Parse(Request["sEcho"]);
            int pageSize = Request["iDisplayLength"] == null ? 15 : int.Parse(Request["iDisplayLength"]);
            int pageStart = Request["iDisplayStart"] == null ? 0 : int.Parse(Request["iDisplayStart"]);

            string SortDirindex = Request["iSortCol_0"] == null ? "" : Request["iSortCol_0"];
            string SortDir = "";
            string DataProp = "";
            if (!string.IsNullOrEmpty(SortDirindex))
            {
                DataProp = Request["mDataProp_" + SortDirindex] == null ? "" : Request["mDataProp_" + SortDirindex];
                SortDir = Request["sSortDir_0"] == null ? "" : Request["sSortDir_0"]; //asc or desc
            }
            string provinceID = Request["provinceid"] == null ? "0" : Request["provinceid"];
            string cityname = Request["cityname"] == null ? "" : Request["cityname"].Trim();
            string IsActive = Request["isactive"] == null ? "0" : Request["isactive"];

            int totalcount = 0;

            List<Exam.Model.ExtentionModels.ExCity> listCity = Excityservice.GetCityList(provinceID, cityname, IsActive, DataProp, SortDir=="asc", pageStart, pageSize, out totalcount);


            var result = new { iTotalDisplayRecords = totalcount, iTotalRecords = totalcount, aaData = listCity, sEcho = 0 };
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [AllowAnonymous]
        public string AddCityinfo(City city)
        {
            try
            {
                if (IsExistData(city).CityID.ToString() == "00000000-0000-0000-0000-000000000000")
                {
                    city.CityID = Guid.NewGuid();
                    DateTime time = DateTime.Now;
                    city.CreateTime = time;
                    city.UpdateTime = time;
                    city.CreateUserID = UserAccount.UserID;
                    city = cityService.AddEntity(city);
                    return "suc";
                }
                else
                {
                    return "repeat";
                }

            }
            catch (Exception e)
            {
                return "err";
            }
        }
        [HttpPost]
        [AllowAnonymous]
        public string UpdateCityInfo(City city)
        {
            try
            {
                City _city = IsExistData(city);
                if (_city.CityID.ToString() == "00000000-0000-0000-0000-000000000000" || _city.CityID == city.CityID)
                {
                    city.UpdateTime = DateTime.Now;
                    city.UpdateUserID = UserAccount.UserID;
                    cityService.UpdateEntity(city);

                    return "suc";
                }
                else
                {
                    return "repeat";
                }
            }
            catch (Exception e)
            {
                return "err";
            }
        }

        /// <summary>
        /// 根据省ID获取城市
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
       [AllowAnonymous]
        public JsonResult GetCityByProvinceID(string id)
        {
            Guid guid;
            if (Guid.TryParse(id, out guid))
            {
                IQueryable<City> listcity = cityService.LoadEntites(e => e.ProvinceID == guid);
                return Json(listcity, JsonRequestBehavior.AllowGet);
            }

            return Json("", JsonRequestBehavior.AllowGet);
        }
        #region 公用
        public City IsExistData(City city)
        {
            SQLHelper commonservice = new SQLHelper();
            Dictionary<string, SqlParameter> dic = new Dictionary<string, SqlParameter>();

            SqlParameter parname = new SqlParameter("@CityName", System.Data.SqlDbType.NVarChar);
            parname.Value = city.CityName;
            dic.Add("CityName", parname);

            SqlParameter provinceID = new SqlParameter("@ProvinceID", System.Data.SqlDbType.UniqueIdentifier);
            provinceID.Value = city.ProvinceID;
            dic.Add("ProvinceID", provinceID);

            return commonservice.getExistData<City>("City", dic);

        }

        #endregion
    }
}
